package com.linewow.xhyy.nihaonew.control;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.activity.NewTopicActivity;
import com.linewow.xhyy.nihaonew.activity.account.LoginActivity;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.entity.FrameActData;
import com.linewow.xhyy.nihaonew.fragment.AttentionFragment;
import com.linewow.xhyy.nihaonew.fragment.FindFragment;
import com.linewow.xhyy.nihaonew.fragment.MessageFragment;
import com.linewow.xhyy.nihaonew.fragment.MineFragment;
import com.linewow.xhyy.nihaonew.model.AppConstants;

/**
 * Created by LXR on 2016/8/23.
 */
public class FrameActDataControl {
    public FrameActData frameActData;
    private FragmentManager fragmentManager;
    private FindFragment findFragment;
    private AttentionFragment attentionFragment;
    private MessageFragment messageFragment;
    private MineFragment mineFragment;
    private String TAG="FrameActDataControl";

    public FrameActDataControl(FragmentManager fragmentManager) {
        frameActData=new FrameActData();
        this.fragmentManager = fragmentManager;
        showFragment(AppConstants.MODULE_FIND);
    }

    public void find(View view){
        Log.e(TAG,"find点击了");
        goneFragment();
        showFragment(AppConstants.MODULE_FIND);
    }

    public void attention(View view){
        Log.e(TAG,"attention点击了");
        goneFragment();
        showFragment(AppConstants.MODULE_ATTENTION);
    }
    public void message(View view){
        goneFragment();
        showFragment(AppConstants.MODULE_MESSAGE);
    }
    public void mine(View view){
        goneFragment();
        showFragment(AppConstants.MODULE_MINE);
    }
    public void newTopic(View view){
        if(NiHaoApp.flag){
            Intent intent=new Intent(findFragment.getActivity(), NewTopicActivity.class);
            findFragment.getActivity().startActivity(intent);
        }else{
            Intent intent=new Intent(findFragment.getActivity(), LoginActivity.class);
            findFragment.getActivity().startActivity(intent);
        }

    }






    private void goneFragment() {
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        if(findFragment!=null){
            fragmentTransaction.hide(findFragment);
        }
        if(attentionFragment!=null){
            fragmentTransaction.hide(attentionFragment);
        }
        if(messageFragment!=null){
            fragmentTransaction.hide(messageFragment);
        }
        if(mineFragment!=null){
            fragmentTransaction.hide(mineFragment);
        }
        fragmentTransaction.commit();
    }

    private void showFragment(int module) {
        frameActData.module.set(module);//更新界面
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        switch (module){
            case AppConstants.MODULE_FIND:
                if(findFragment==null){
                    findFragment=new FindFragment();
                    fragmentTransaction.add(R.id.frame_act_fragment,findFragment);
                }else{
                    fragmentTransaction.show(findFragment);
                }
                break;
            case AppConstants.MODULE_ATTENTION:
                if(attentionFragment==null){
                    attentionFragment=new AttentionFragment();
                    fragmentTransaction.add(R.id.frame_act_fragment,attentionFragment);
                }else{
                    fragmentTransaction.show(attentionFragment);
                }
                break;
            case AppConstants.MODULE_MESSAGE:
                if(messageFragment==null){
                    messageFragment=new MessageFragment();
                    fragmentTransaction.add(R.id.frame_act_fragment,messageFragment);
                }else{
                    fragmentTransaction.show(messageFragment);
                }
                break;
            case AppConstants.MODULE_MINE:
                if(mineFragment==null){
                    mineFragment=new MineFragment();
                    fragmentTransaction.add(R.id.frame_act_fragment,mineFragment);
                }else{
                    fragmentTransaction.show(mineFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    public void setMessageNum(int num){
        frameActData.message.set(num);
    }
}
